<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://think.ctolog.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\xueao\controller;

use controller\XueaoAdmin;
use think\Db;

/**
 * Class Article
 * @package app\jujin\controller
 */
class Article extends XueaoAdmin
{

    /**
     * 定义当前操作表名
     * @var string
     */
    public $table = 'Article';

    /**
     * @return array|string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $this->title = '文章列表';
        $get = $this->request->get();
        $db = Db::name($this->table);
        if (isset($get['type']) && is_numeric($get['type'])) {
            $db->where('type', $get['type']);
        }
        if (isset($get['title']) && $get['title']!='') {
            $db->whereLike('title', '%'.$get['title'].'%');
        }
        if (isset($get['create_at']) && $get['create_at'] !== '') {
            list($start, $end) = explode(' - ', $get['create_at']);
            $db->whereBetween('create_at', [strtotime("{$start} 00:00:00"), strtotime("{$end} 23:59:59")]);
        }
        $this->_form_assign();
        return parent::_list($db->order('sort,id desc'));
    }

    public function _data_filter(&$list)
    {
        foreach ($list as $k=>&$v){
            $v['type']=app('systemCateService')::get_cate_by_id($v['type'],'title');
        }
    }

    private function _form_assign()
    {
        //查询文章分类
        $cateWhere = ['status' => '1', 'pid' => 4];
        $this->assign('cates',app('systemCateService')::get_cate_list($cateWhere));
    }

    protected function _form_filter(&$data,$extend_data)
    {
        if($this->request->isGet()){
            if($this->request->get('id')){
                $this->title='编辑文章';
                $data['childs']=explode(',',$data['childs']);
            }else{
                $this->title='添加文章';
            }
            $this->_form_assign();
        }else{
            if(!$this->request->post('id')){
                $data['status']=1;
                $data['create_at']=time();
            }

            if($this->request->post('childs')!==NULL){
                $childs=$this->request->post('childs');
                if(!isset($childs[1])){
                    $this->error('子分类不能为空');
                }else{
                    $data['childs']=implode(',',$childs);
                }
            }
        }
    }

}
